package org.serverct.finalfight.game.listener.player;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.serverct.finalfight.game.GameManager;
import org.serverct.finalfight.internal.game.basic.BasicGame;
import org.serverct.finalfight.internal.game.basic.GameState;

public class BuildListener implements Listener {
    @EventHandler
    public void onQuit(PlayerQuitEvent event) {
        Player player = event.getPlayer();
        BasicGame game = GameManager.getGame(player);
        if (game == null) {
            return;
        }
        game.quit(player, true);
    }

    @EventHandler
    public void onBreak(BlockBreakEvent event) {
        Player player = event.getPlayer();
        BasicGame game = GameManager.getGame(player);
        if (game == null) {
            return;
        }
        if (game.state != GameState.RUNNING) {
            event.setCancelled(true);
        }
    }

    @EventHandler
    public void onPlace(BlockPlaceEvent event) {
        Player player = event.getPlayer();
        BasicGame game = GameManager.getGame(player);
        if (game == null) {
            return;
        }
        if (game.state != GameState.RUNNING) {
            event.setCancelled(true);
        }
    }

    @EventHandler
    public void onClick(PlayerInteractEvent event) {
        Player player = event.getPlayer();
        BasicGame game = GameManager.getGame(player);
        if (game == null) {
            return;
        }
        if (game.state != GameState.RUNNING) {
            event.setCancelled(true);
        }
    }
}
